package com.jz.yanshe.ui;

import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jz.yanshe.base.BaseActivity;

import com.jz.yanshe.R;

import com.jz.yanshe.dialog.AreaDialogFragment;
import com.jz.yanshe.util.PreferencesHelper;
import com.jz.jd.rest.business.RestProxy;
import com.jz.jd.rest.exception.BusinessException;
import com.jz.jd.rest.response.AddressResponse;
import com.jz.jd.rest.response.BaseResponse;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by Administrator on 2018/3/9.
 */

public class AddAddressActivity extends BaseActivity {

    private AreaDialogFragment mAreaDialogFragment;
    private String ids;
    private int type;
    private AddressResponse.Address address;

    @BindView(R.id.tv_user_name)
    EditText tv_user_name;

    @BindView(R.id.tv_user_phone)
    EditText tv_user_phone;

    @BindView(R.id.tv_user_address)
    EditText tv_user_address;

    @BindView(R.id.btn_area)
    TextView btn_area;

    @Override
    protected void initViews() {

    }

    @Override
    protected void initData() {
        address = (AddressResponse.Address) getIntent().getSerializableExtra("obj");
        if (address == null) {
            type = 0;
        } else {
            type = 1;
            tv_user_name.setText(address.nick_name);
            tv_user_phone.setText(address.phone);
            tv_user_address.setText(address.address_detail);
            btn_area.setText(address.address_ids_name);
            ids = address.address_ids;
        }
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_add_address;
    }

    @OnClick(R.id.btn_area)
    void btn_area() {
        showAreaDialog();
    }

    @OnClick(R.id.btn_add)
    void btn_add() {
        if (type == 0) {
            addAddress();
        }else{
            updateAddress();
        }
    }


    private void showAreaDialog() {
        if (mAreaDialogFragment == null) {
            mAreaDialogFragment = mAreaDialogFragment.newInstance();
            mAreaDialogFragment.show(getSupportFragmentManager(), "mAreaDialogFragment");
            mAreaDialogFragment.setmListener(new AreaDialogFragment.OnItemClickListener() {
                @Override
                public void onConfirm(String ids, String address) {
                    AddAddressActivity.this.ids = ids;
                    btn_area.setText(address);
                }
            });
        } else if (!mAreaDialogFragment.isAdded() && !mAreaDialogFragment.isShowing()) {
            mAreaDialogFragment.show(getSupportFragmentManager(), "mAreaDialogFragment");
        }
    }

    private void addAddress() {
        if(ids.length()<6){
            Toast.makeText(this,"请选择完整省市区",Toast.LENGTH_SHORT).show();
            return;
        }
        RestProxy.getInstance().addAddress(PreferencesHelper.getInstance().getString(AddAddressActivity.this,"token"), ids, tv_user_address.getText().toString(), tv_user_phone.getText().toString(), tv_user_name.getText().toString(), new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                Toast.makeText(AddAddressActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                finish();
            }

            @Override
            public void onError(Throwable e) {

                if (e instanceof BusinessException) {
                toLogin(((BusinessException)e).errorCode);}
            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void updateAddress() {
        RestProxy.getInstance().update(PreferencesHelper.getInstance().getString(AddAddressActivity.this,"token"),address.user_address_id, ids, tv_user_address.getText().toString(), tv_user_phone.getText().toString(), tv_user_name.getText().toString(), new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                Toast.makeText(AddAddressActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                finish();
            }

            @Override
            public void onError(Throwable e) {

                if (e instanceof BusinessException) {
                toLogin(((BusinessException)e).errorCode);}
            }

            @Override
            public void onComplete() {

            }
        });
    }
}
